package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.MerGroupInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.MerGroupInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.MerGroupInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.IndustryInfoBean;
import cc.rengu.igas.bomp.facade.request.IndustryGroupInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.IndustryGroupInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 行业组别查询.
 *
 * @author zhangxr
 * @version 1.0
 * @date 2020.04.23
 */
public class IndustryGroupInfoQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        IndustryGroupInfoQueryRequest industryGroupInfoQueryRequest = new IndustryGroupInfoQueryRequest();
        ConvertUtil.convertOutput(industryGroupInfoQueryRequest);
        return industryGroupInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        IndustryGroupInfoQueryRequest queryRequest = (IndustryGroupInfoQueryRequest) request;
        BizResponse<IndustryGroupInfoQueryResponse> bizResponse = new BizResponse<>();
        IndustryGroupInfoQueryResponse queryResponse = new IndustryGroupInfoQueryResponse();

        int pageIndex = queryRequest.getPageIndex();
        int pageSize = queryRequest.getPageSize();
        List<MerGroupInfo> merGroupInfoList = new ArrayList<>();
        if (-1 == pageIndex || -1 == pageSize) {
            MerGroupInfoMapper merGroupInfoMapper = new MerGroupInfoMapperImpl();
            merGroupInfoList = merGroupInfoMapper.selectMerGroupInfoByCondition(queryRequest.getIndustryCode(), queryRequest.getIndustryName());
            if (CollectionUtils.isEmpty(merGroupInfoList)) {
                rglog.info("根据查询条件查询到0条行业类别");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                bizResponse.setResult(queryResponse);
                return bizResponse;
            }

        } else {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex);
            pageInfo.setPageSize(pageSize);
            MerGroupInfoMapper merGroupInfoMapper = new MerGroupInfoMapperImpl();
            PageResult pageResult = merGroupInfoMapper.selectMerGroupInfoPageByCondition(queryRequest.getIndustryCode(), queryRequest.getIndustryName(), pageInfo);

            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                rglog.info("根据查询条件查询到0条行业类别");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                bizResponse.setResult(queryResponse);
                return bizResponse;
            }

            /* 根据取出记录条数做对应处理 */
            if (pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条行业类别!");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                bizResponse.setResult(queryResponse);
                return bizResponse;
            } else {
                for (Object object : pageResult.getResult()) {
                    merGroupInfoList.add((MerGroupInfo) object);
                }
                rglog.info("SIZE=<{}>", pageResult.getResult().size());
                queryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                queryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
            }
        }


        if (!CollectionUtils.isEmpty(merGroupInfoList)) {
            /* 根据分页查询条件截取相关数据记录 */
            List<IndustryInfoBean> industryInfoBeans = new ArrayList<>();
            merGroupInfoList.forEach(item -> convertIndustryInfoBean(item, industryInfoBeans));
            /* 查询结果赋值 */
            queryResponse.setIndustryInfoList(industryInfoBeans);
        } else {
            rglog.debug("根据输入的组别编号、产品标识、查询商户组别信息为空");
            queryResponse.setTotalRows(0);
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            bizResponse.setResult(queryResponse);
            return bizResponse;
        }

        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(queryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        IndustryGroupInfoQueryResponse industryGroupInfoQueryResponse = (IndustryGroupInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(industryGroupInfoQueryResponse);
    }


    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param merGroupInfo         数据库表结构对象
     * @param industryInfoBeanList 接口对象
     */
    private void convertIndustryInfoBean(MerGroupInfo merGroupInfo, List<IndustryInfoBean> industryInfoBeanList) {
        if ("01".equals(merGroupInfo.getStatus())) {
            IndustryInfoBean industryInfoBean = new IndustryInfoBean();
            industryInfoBean.setIndustryCode(merGroupInfo.getGrpNo());
            industryInfoBean.setIndustryName(merGroupInfo.getGrpDesc());
            industryInfoBeanList.add(industryInfoBean);
        }
    }
}
